﻿using System.Linq;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;
using Arch.Common.Mapping;
using Microsoft.Practices.Unity;
using Arch.Client.IoC;

namespace Arch.Client.Services
{
    public class ClientNotifyProxyFormatter : IClientMessageFormatter
    {
        [Dependency(ClientDependencyContainer.ServiceToClientCode)]
        public IMapper ServiceToClientMapper { get; set; }

        [Dependency(ClientDependencyContainer.ClientToServiceCode)]
        public IMapper ClientToServiceMapper { get; set; }

        readonly IClientMessageFormatter _original;

        public ClientNotifyProxyFormatter(IClientMessageFormatter original)
        {
            _original = original;
        }

        public Message SerializeRequest(MessageVersion messageVersion, object[] parameters)
        {
            return _original.SerializeRequest(messageVersion, parameters.Select(o => ClientToServiceMapper.Map(o)).ToArray());
        }

        public object DeserializeReply(Message message, object[] parameters)
        {
            var results = _original.DeserializeReply(message, parameters);
            return ServiceToClientMapper.Map(results);
        }
    }
}